package cn.lutu.domain.service.impl;


import cn.lutu.dao.mapper.SpotMapper;
import cn.lutu.dao.mapper.SpotTicketMapper;
import cn.lutu.domain.repository.SpotRepository;
import cn.lutu.domain.repository.SpotTicketRepository;
import cn.lutu.domain.repository.impl.AdminRepositoryImpl;
import cn.lutu.domain.service.SpotService;
import cn.lutu.protocol.dto.RoleAddDTO;
import cn.lutu.protocol.dto.SpotAddDTO;
import cn.lutu.protocol.dto.SpotDTO;
import cn.lutu.protocol.dto.SpotUpdateDTO;
import cn.lutu.protocol.entity.Spot;
import cn.lutu.protocol.entity.SpotAndTicket;
import cn.lutu.protocol.entity.UserAndRole;
import cn.lutu.protocol.vo.*;
import cn.lutu.protocol.vo.adminVO.AdminSpotVO;
import cn.lutu.protocol.vo.adminVO.RoleUpdateInfoVO;
import cn.lutu.util.exception.ServiceException;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.StatusCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SpotServiceImpl implements SpotService {

    @Value("${lutu.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    SpotMapper mapper;

    @Autowired
    SpotRepository spotRepository;
    @Autowired
    AdminRepositoryImpl adminRepository;
    @Autowired
    SpotTicketRepository spotTicketRepository;

    @Override
    public void insert(SpotDTO spotDTO) {
        Spot spot = new Spot();
        BeanUtils.copyProperties(spotDTO, spot);
        mapper.insert(spot);
    }

    @Override
    public void update(SpotDTO spotDTO) {
        Spot spot = new Spot();
        BeanUtils.copyProperties(spotDTO, spot);
        QueryWrapper<Spot> wrapper = new QueryWrapper<>();

        mapper.update(spot,wrapper);
    }

    @Override
    public PageData<SpotVO> selectSpotList(Integer pageNum, Integer pageSize) {
        log.debug("正在处理获取景区列表业务");
        PageData<SpotVO> pageData = spotRepository.selectSpotList(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<SpotVO> selectSpotList(Integer pageNum) {
        log.debug("正在处理获取酒店列表业务");
        PageData<SpotVO> pageData = spotRepository.selectSpotList(pageNum,defaultQueryPageSize);
        return pageData;
    }

  /*  @Override
    public void delete(Long id) {
        mapper.delete(id);
    }*/

   /* @Override
    public SpotVO select(Long id) {
        SpotVO spotVO = mapper.select(id);
        return spotVO;
    }*/



    @Override

    public SpotVO selectSpotLIstById(Long id) {
        log.debug("根据ID查询景区数据，参数{}",id);
        return spotRepository.selectSpotListById(id);
    }

    @Override
    public List<VoteVO> selectVoteById(Long id) {
        log.debug("根据ID查询门票数据，参数：{}",id);
        List<VoteVO> list = spotRepository.selectVoteById(id);
        return list;
    }

    @Override
    public void updateSpotById(SpotUpdateDTO spotUpdateDTO) {
        log.debug("开始处理【修改景点】的业务，参数：{}", spotUpdateDTO);

        Long id = spotUpdateDTO.getId();
        SpotVO spotVO = spotRepository.selectSpotListById(id);
        if (spotVO == null) {
            String message = "修改景点失败，尝试修改的景点数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }
        Spot spot = new Spot();
        BeanUtils.copyProperties(spotUpdateDTO, spot);
        SpotAndTicket spotAndTicket =new SpotAndTicket();
        SpotTicketVO spotTicketVO =spotTicketRepository.selectInfo(spotUpdateDTO.getId(),spotUpdateDTO.getCategoryId());
        log.debug("spotTicketVO++"+spotTicketVO);
        spotAndTicket.setId(spotTicketVO.getId());
        spotAndTicket.setSpotId(spotUpdateDTO.getId());
        spotAndTicket.setCategoryId(spotUpdateDTO.getCategoryId());
        spotAndTicket.setPrice(spotUpdateDTO.getPrice());
        spotTicketRepository.updateById(spotAndTicket);
        int rows = adminRepository.updateSpotById(spot);
        if (rows != 1) {
            String message = "修改景点失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.OPERATION_FAILED, message);
        }

    }

    @Override
    public void deleteSpotById(Long id) {
        log.info("开始执行根据ID删除景点的业务");
        SpotVO spotVO = spotRepository.selectSpotListById(id);
        if (spotVO == null) {
            String message = "删除景点失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }

        int rows = spotRepository.deleteSpotById(id);
        if (rows != 1) {
            String message = "删除景点失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UNKNOWN, message);
        }
    }

    @Transactional
    @Override
    public void addSpot(SpotAddDTO spotAddDTO) {
        log.info("开始执行新增景点的功能，名称为：{}",spotAddDTO);
        if (spotAddDTO == null) {
            String message = "您输入的数据为空!";
            log.debug(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND,message);
        }
        Spot spot =new Spot();
        BeanUtils.copyProperties(spotAddDTO,spot);
        spotRepository.addSpot(spot);
        SpotAndTicket spotAndTicket=new SpotAndTicket();
        spotAndTicket.setSpotId(spot.getId());
        spotAndTicket.setCategoryId(spotAddDTO.getCategoryId());
        spotAndTicket.setPrice(spotAddDTO.getPrice());
        spotRepository.addSpotTicket(spotAndTicket);
    }

    @Override
    public List<SpotVO> selectAllList() {
        List<SpotVO> list = spotRepository.selectAllList();
        return list;
    }

    @Override
    public List<SpotVO> selectIndex() {
        List<SpotVO> list = spotRepository.selectIndex();
        return list;
    }


}
